var bSubmitting = false;
function validate()
{
    bSubmitting = true;
    $("submit_btn").disabled = true;
    
    // page title
    page_title = $("annoration_title").value;
    if (page_title == "")
    {
        alert("Please enter a title for the web page.");
        $("submit_btn").disabled = false;
        bSubmitting = false;
        return false;
    }
    // page address
    page_uri = $("annoration_uri").value;
    if (page_uri == "")
    {
        alert("Please enter the address of the web page to annorate.");
        $("submit_btn").disabled = false;
        bSubmitting = false;
        return false;
    }
    history.back(1);
    return true;
}
var nSpecifiedRating = 0;
var bInStars = false;
function overStar(nRating)
{
    if (bSubmitting) return;
    bInStars = true;
    showRating(nRating);
}
function showRating(nRating)
{
	var nStar;
    for (nStar = 1; nStar < 6; nStar++)
    {
        star = document.getElementById("star_" + nStar);
        star.src = "/images/star_" + (nStar <= nRating ? "filled" : "empty") + "_big.gif";
    }
}
function setRating(nRating)
{
    document.getElementById("rating").value=nRating;
}
function clickStar(nRating)
{
    if (bSubmitting) return;
    if (nSpecifiedRating == 1 && nRating == 1)
    {
        nSpecifiedRating = 0;
        setRating(0);
        showRating(0);
    }
    else
    {
        nSpecifiedRating = nRating;
        setRating(nRating);
    }
}
function change()
{
    if (!bInStars) showRating(nSpecifiedRating);
}
function leaveStars()
{
    if (bSubmitting) return;
    bInStars = false;
    setTimeout("change()",100);
}
function overStars()
{
    if (bSubmitting) return;
    bInStars = true;
}
function leaveStar()
{
    if (bSubmitting) return;
    bInStars = false;
}
function replaceAll(text, strA, strB)
{
    return text.replace( new RegExp(strA,"g"), strB );    
}

function trimString (str) 
{
    str = this != window? this : str;
    return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
String.prototype.trim = trimString;
    
function mouseOverTag(id)
{
    if (bSubmitting) return;
    suggestedTag = $("suggested_tag_" + id); 
    if (suggestedTag)
    {
        sClassName = suggestedTag.className;
        if (sClassName.indexOf("tag_selected") == -1) suggestedTag.className = "suggested_tag_hover";
    }
    popularTag = $("popular_tag_" + id);
    if (popularTag)
    {
        sClassName = popularTag.className;
        if (sClassName.indexOf("tag_selected") == -1) popularTag.className = "popular_tag_hover";
    }
  }

function mouseOutTag(id)
{
    if (bSubmitting) return;
    suggestedTag = $("suggested_tag_" + id); 
    if (suggestedTag)
    {
        sClassName = suggestedTag.className;
        if (sClassName.indexOf("hover") != -1) suggestedTag.className = "suggested_tag_unselected";
    }
    popularTag = $("popular_tag_" + id);
    if (popularTag)
    {
        sClassName = popularTag.className;
        if (sClassName.indexOf("hover") != -1) popularTag.className = "popular_tag_unselected";
    }
}

function chooseTag(prefix,name,id)
{
    if (name.indexOf(' ') != -1) name = '"' + name + '"';
    var sCurrentTags = new String($("page_tags").value).trim();
    sClassName = $(prefix + "_tag_" + id).className;
    if (sClassName.indexOf("tag_selected") != -1)
    {
        if ($("suggested_tag_" + id)) $("suggested_tag_" + id).className = "suggested_tag_unselected";
        if ($("popular_tag_" + id)) $("popular_tag_" + id).className = "popular_tag_unselected";
        $("page_tags").value = replaceAll(replaceAll(sCurrentTags, name, ""),"  "," ").trim();
    }
    else
    {
        if ($("suggested_tag_" + id)) $("suggested_tag_" + id).className = "suggested_tag_selected";
        if ($("popular_tag_" + id)) $("popular_tag_" + id).className = "popular_tag_selected";
        sCurrentTags += " " + name;
        $("page_tags").value = sCurrentTags.trim();
    }
    $("page_tags").focus();
}
function $(id)
{
    return document.getElementById(id);
}
function chooseSuggestedTag(name,id)
{
    chooseTag("suggested",name,id);
}

function choosePopularTag(name,id)
{
    chooseTag("popular",name,id);
}

document.getElementsByClassName = function(className, parentElement) {
  if (Prototype.BrowserFeatures.XPath) {
    var q = ".//*[contains(concat(' ', @class, ' '), ' " + className + " ')]";
    return document._getElementsByXPath(q, parentElement);
  } else {
    var children = ($(parentElement) || document.body).getElementsByTagName('*');
    var elements = [], child;
    for (var i = 0, length = children.length; i < length; i++) {
      child = children[i];
      if (Element.hasClassName(child, className))
        elements.push(Element.extend(child));
    }
    return elements;
  }
};